<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect @getCategoryInfo="getCategoryInfo" :isAddOrUpdate="scene != 0"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="scene == 0">
        <!-- 添加按钮 -->
        <el-button type="primary" icon="el-icon-plus" :disabled="!categoryForm.category3Id" round
          @click="addOrUpdateAttr(categoryForm.category3Id)">添加SPU</el-button>
        <!-- 显示的table -->
        <el-table style="width: 100%;" border :data="SPUList">
          <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width"></el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width"></el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 使用自定义的按钮 -->
              <FengButton type="success" icon="el-icon-plus" size="mini" title="添加sku" @click="addSKUInfo(row)"></FengButton>
              <FengButton type="warning" icon="el-icon-edit" size="mini" title="修改spu" @click="addOrUpdateAttr(row)">
              </FengButton>
              <FengButton type="info" icon="el-icon-info" size="mini" title="查看当前spu全部sku列表" @click="viewSKUList(row)"></FengButton>
              <el-popconfirm title="确认要删除该条数据吗？" @onConfirm="deleteSPU(row)">
                <FengButton type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference" ></FengButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
          :page-sizes="[3, 5, 15]" :page-size="pageSize" layout="prev, pager, next, jumper,->,sizes,total"
          :total="total" :pager-count="7" style="margin-top: 20px; text-align: center;">
        </el-pagination>
      </div>
      <SPUForm v-show="scene == 1" ref="spuForm" @changeScene="changeScene"></SPUForm>
      <SKUForm v-show="scene == 2" ref="skuForm" @changeScene="changeScene"></SKUForm>
    </el-card>

    <el-dialog :title="`${spuInfo.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="closeDialog">
      <el-table :data="SKUList"  style="width: 100%" border v-loading="skuListloading">
        <el-table-column prop="skuName" label="名称" width="width"></el-table-column>
        <el-table-column prop="price" label="价格" width="width"></el-table-column>
        <el-table-column prop="weight" label="重量" width="width"></el-table-column>
        <el-table-column prop="prop" label="默认图片" width="width">
          <template slot-scope="{ row, $index }">
             <img :src="row.skuDefaultImg" style="width:100px;height:100px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import SKUForm from './skuForm';
import SPUForm from './spuForm';
export default {
  name: "SPUManager",
  components: {
    SKUForm, SPUForm
  },
  data() {
    return {
      scene: 0,
      categoryForm: {
        category1Id: '',
        category2Id: '',
        category3Id: '',
      },
      SPUList: [],
      pageSize: 3,
      currentPage: 1,
      total: 0,
      dialogTableVisible:false,
      spuInfo:{},
      SKUList:[],
      skuListloading:true,

    }
  },
  mounted() {

  },
  methods: {
    getCategoryInfo(val) {
      this.categoryForm = val;
      if (val.category3Id) {
        this.getSPUList();
      } else {
        this.SPUList = [];
      }
    },
    //获取SKU列表
    async getSPUList() {
      const result = await this.$API.spu.reqSpuList(this.currentPage, this.pageSize, this.categoryForm.category3Id);
      if (result.code == 200) {
        this.SPUList = result.data.records;
        this.total = result.data.total;
      }
    },
    //分页当PageSize改变时
    handleSizeChange(val) {
      this.pageSize = val;
      this.getSPUList();
    },
    //当前页改变时
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSPUList();
    },
    //添加或更新SPU
    addOrUpdateAttr(val) {
      this.scene = 1;
      this.$refs.spuForm.initData(val);

    },
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag == "") return;
      this.getSPUList();
    },
    //删除SKU
    async deleteSPU(row){
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if(result.code==200){
        this.$message({ type: "success", message: "删除成功" });

        if(this.SPUList.length<=1){
          this.currentPage=this.currentPage>1?this.currentPage-1:1;
        }
        this.getSPUList();
      }
    },
    //添加SKU
    addSKUInfo(row){
      this.scene=2;
      this.$refs.skuForm.initData(row,this.categoryForm)
    },
    //查看已经添加的SKU的列表
    async viewSKUList(row){
      this.dialogTableVisible=true;
      this.spuInfo=row;
      let result = await this.$API.spu.reqSkuList(row.id);
      if (result.code == 200) {
        this.SKUList = result.data;
        this.skuListloading=false;
      }
    },
    //关闭对话框
    closeDialog(done){
      this.skuListloading = true;
      this.SKUList=[];
      done();
    }
  }
};
</script>

<style lang="scss" scoped></style>
